
package com.shop.cloud.mall.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.log.annotation.SysLog;
import com.shop.cloud.mall.admin.service.ActiveBorderService;
import com.shop.cloud.mall.common.entity.ActiveBorder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 活动边框
 *
 * @author
 * @date 2023-03-22 17:16:20
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/activeborder")
@Tag(description = "activeborder", name = "活动边框管理")
public class ActiveBorderController {

    private final ActiveBorderService activeBorderService;

    /**
     * 活动边框分页列表
     * @param page 分页对象
     * @param activeBorder 活动边框
     * @return
     */
    @Operation(summary = "活动边框分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ato.hasAuthority('mall:activeborder:index')")
    public R getPage(Page page, ActiveBorder activeBorder) {
        return R.ok(activeBorderService.page(page, Wrappers.query(activeBorder)));
    }

    /**
     * 活动边框查询
     * @param id
     * @return R
     */
    @Operation(summary = "活动边框查询")
    @GetMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:activeborder:get')")
    public R getById(@PathVariable("id") String id) {
        return R.ok(activeBorderService.getById2(id));
    }

    /**
     * 活动边框新增
     * @param activeBorder 活动边框
     * @return R
     */
    @Operation(summary = "活动边框新增")
    @SysLog("新增活动边框")
    @PostMapping
    @PreAuthorize("@ato.hasAuthority('mall:activeborder:add')")
    public R save(@RequestBody ActiveBorder activeBorder) {
    	//校验是否有时间段重合的活动
		List<ActiveBorder> listActiveBorder = activeBorderService.list(Wrappers.<ActiveBorder>query().lambda()
				.nested(i -> i.ge(ActiveBorder::getValidBeginTime,activeBorder.getValidBeginTime()).le(ActiveBorder::getValidBeginTime,activeBorder.getValidEndTime()))
				.or(i -> i.le(ActiveBorder::getValidBeginTime,activeBorder.getValidBeginTime()).ge(ActiveBorder::getValidEndTime,activeBorder.getValidEndTime()))
				.or(i -> i.ge(ActiveBorder::getValidEndTime,activeBorder.getValidBeginTime()).le(ActiveBorder::getValidEndTime,activeBorder.getValidEndTime()))
		);
		if(listActiveBorder != null && listActiveBorder.size() > 0){
			return R.failed("该时间段有其他活动边框");
		}
        return R.ok(activeBorderService.save(activeBorder));
    }

    /**
     * 活动边框修改
     * @param activeBorder 活动边框
     * @return R
     */
    @Operation(summary = "活动边框修改")
    @SysLog("修改活动边框")
    @PutMapping
    @PreAuthorize("@ato.hasAuthority('mall:activeborder:edit')")
    public R updateById(@RequestBody ActiveBorder activeBorder) {
        return R.ok(activeBorderService.updateById1(activeBorder));
    }

    /**
     * 活动边框删除
     * @param id
     * @return R
     */
    @Operation(summary = "活动边框删除")
    @SysLog("删除活动边框")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:activeborder:del')")
    public R removeById(@PathVariable String id) {
        return R.ok(activeBorderService.removeById(id));
    }

}
